package com.bvblogic.android.net;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class JSONHTTP {
	
	public static final int HTTP_RESPOND_TIMEOUT = 10000;
	
    /**
     * request method which do HttpGet sending given parameters
     * converted to String for HttpGet 
     * @param sURL
     * @param params
     * @return String with response
     */
	public static String doGet(final String sURL, final ArrayList<NameValuePair> params){
		if (sURL==null || sURL.length()==0)
			return null;
        final HttpClient httpclient= new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), HTTP_RESPOND_TIMEOUT);
        String sURLWithParams = sURL;
		if (params!=null && params.size()>0)
			sURLWithParams = sURLWithParams +"?"+ URLEncodedUtils.format(params, "UTF-8");
        final HttpGet mHttpRequest = new HttpGet (sURLWithParams);
        mHttpRequest.setHeader("Accept", "application/json");
        mHttpRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
        final HttpResponse responseHTTP;
		String responseString = null;
        try {
            responseHTTP = httpclient.execute(mHttpRequest);
            responseString = EntityUtils.toString(responseHTTP.getEntity());
        } 
        catch (final ClientProtocolException e) {
            e.printStackTrace();
	    	return null;
        }
        catch (final UnsupportedEncodingException e) {
			e.printStackTrace();
	    	return null;
	    } 
        catch (final ParseException e) {
            e.printStackTrace();
	    	return null;
        }
	    catch (final IOException e) {
	        e.printStackTrace();
	    	return null;
	    }
 	    return responseString;
	}

	/**
     * request method which do HttpPost sending given parameters 
     * converted to HttpPost parameters 
     * @param sURL
     * @param params
     * @return String with response
     */
	public static String doPost(String sURL, ArrayList<NameValuePair> params){
		if (sURL==null || sURL.length()==0)
			return null;
        final HttpClient httpclient= new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), HTTP_RESPOND_TIMEOUT);
        final HttpPost mHttpRequest = new HttpPost (sURL);
		if (params!=null && params.size()>0)
	        try {
	        	mHttpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			} 
			catch (final UnsupportedEncodingException e) {
				e.printStackTrace();
				return null;
			} 	
        mHttpRequest.setHeader("Accept", "application/json");
        mHttpRequest.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        final HttpResponse responseHTTP;
		String responseString = null;
        try {
            responseHTTP = httpclient.execute(mHttpRequest);
            responseString = EntityUtils.toString(responseHTTP.getEntity());
        } 
        catch (final ClientProtocolException e) {
            e.printStackTrace();
	    	return null;
        }
        catch (final UnsupportedEncodingException e) {
			e.printStackTrace();
	    	return null;
	    } 
        catch (final ParseException e) {
            e.printStackTrace();
	    	return null;
        }
	    catch (final IOException e) {
	        e.printStackTrace();
	    	return null;
	    }
 	    return responseString;
	}	

}
